package com.tz.movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.Movie;
import com.tz.movie.domain.MovieExample;
 
import com.tz.movie.domain.status;
import com.tz.movie.mapper.MovieMapper;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/movie")
public class MovieController {
  @Autowired
  MovieMapper movieMapper;

  @PostMapping(value = "/getAllMovie")
  public String getAllMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    // if (session.getAttribute("userbase") == null) {
    //   session.setAttribute("userbase", null);
    //   return result.toJSONString();
    // }
    // JSONObject jsonObject = JSONObject.parseObject((String) session.getAttribute("moviebase"));
    // if (jsonObject == null || !jsonObject.get("groupid").equals(1)) {
    //   result.put("result", status.s_error);
    //   result.put("groupid", jsonObject.get("groupid"));

    //   return result.toJSONString();
    // }
    MovieExample movieExample = new MovieExample();
    List<Movie> movies = movieMapper.selectByExample(movieExample);
    JSONArray jsonArray = new JSONArray();
    if (movies.size() == 0) {
      result.put("result", status.s_ok);
      result.put("data", movies.get(0));
      return result.toJSONString();
    }
    for (Movie movie : movies) {
      JSONObject obj = JSONObject.parseObject(movie.toString());
      jsonArray.add(obj);
    }
    result.put("result", status.s_ok);

    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/getMovie")
  public String getMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    // if (session.getAttribute("userbase") == null) {
    //   session.setAttribute("userbase", null);
    //   return result.toJSONString();
    // }
    // JSONObject jsonObject = JSONObject.parseObject((String) session.getAttribute("moviebase"));
    // if (jsonObject == null || !jsonObject.get("groupid").equals(1)) {
    //   result.put("result", status.s_error);
    //   result.put("groupid", jsonObject.get("groupid"));

    //   return result.toJSONString();
    // }
    MovieExample movieExample = new MovieExample();
    MovieExample.Criteria criteria = movieExample.createCriteria();
    criteria.andIdEqualTo(jsonparm.getInteger("id"));
    List<Movie> movies = movieMapper.selectByExample(movieExample);
    JSONArray jsonArray = new JSONArray();
    if (movies.size() == 0) {
      result.put("result", status.s_ok);
      result.put("data", movies.get(0));
      return result.toJSONString();
    }
   
    result.put("result", status.s_ok);

    result.put("data", JSONObject.parse(movies.get(0).toString()));
    return result.toJSONString();
  }

  @PostMapping(value = "/updatemovie")
  public String updatemovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    int id = jsonparm.getInteger("id");

    String name = jsonparm.getString("name");
    String info = jsonparm.getString("info");
    String postimg = jsonparm.getString("postimg");
    String videourl = jsonparm.getString("videourl");
    Integer sell = jsonparm.getInteger("sell");
    Integer remain = jsonparm.getInteger("remain");
    Integer cateid = jsonparm.getInteger("cateid");

    Double price = jsonparm.getDouble("price");
 
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
    Movie movie = new Movie();
    movie.setName(name);
    movie.setInfo(info);
    movie.setCateid(cateid);
    movie.setPostimg(postimg);
    movie.setVideourl(videourl);
    movie.setPrice(price);

    MovieExample movieExample = new MovieExample();
    MovieExample.Criteria criteria = movieExample.createCriteria();
    criteria.andIdEqualTo(id);

    movieMapper.updateByExampleSelective(movie, movieExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  @PostMapping(value = "/addMovie")
  public String addMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    String name = jsonparm.getString("name");
    String info = jsonparm.getString("info");
    String postimg = jsonparm.getString("postimg");
    String videourl = jsonparm.getString("videourl");
    Integer remain = jsonparm.getInteger("remain");
    Integer cateid = jsonparm.getInteger("cateid");

    Double price = jsonparm.getDouble("price");
 
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
    Movie movie = new Movie();
    movie.setName(name);
    movie.setInfo(info);
    movie.setCateid(cateid);
    movie.setPostimg(postimg);
    movie.setVideourl(videourl);
    movie.setRemain(remain);
    movie.setSell(0);
    movie.setPrice(price);
    try {
      movie.setOpentime(sdf.parse(jsonparm.getString("opentime")));
      movie.setEndtime(sdf.parse(jsonparm.getString("endtime")));
    }catch (Exception exception)
    {
      System.out.println(exception);
    }
    movieMapper.insert(movie);
    result.put("result", status.s_ok);
    result.put("data", movie.toString());
    return result.toJSONString();
  }

  @PostMapping(value = "/delMovie")
  public String delMovie(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    int delid = jsonparm.getInteger("id");
    MovieExample movieExample = new MovieExample();
    MovieExample.Criteria criteria = movieExample.createCriteria();
    criteria.andIdEqualTo(delid);
    movieMapper.deleteByExample(movieExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }
}
